/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Xiaobao Zeng<zengxiaobao@skyworth.com>
 * Created Time: 2016-09-20 19:37:41
 * Last Modified: 2017-03-26 18:22:09
 * File Name: advertoper_admin.go
 * Description:
 */
package controllers

import (
	"encoding/json"
	"net/url"
	"strconv"

	"advertoper/adoper"
	"advertoper/errorcode"

	log "github.com/cihub/seelog"
)

type AdvertoperAdminController struct {
	SCLoginedController
}

//添加广告运营位元素
//@router /data/create [post]
func (p *AdvertoperAdminController) SaveAdvertoperItem() {
	p.ForceHTTPS()
	defer p.Output("SaveAdvertoperItem")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	reqBody := &adoper.PositionDataInfo{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	code := adoper.GetAdvertoperAdminService().SaveItemRecord(reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)
}

//查看运营位列表
// @router /layouts [post]
func (p *AdvertoperAdminController) ListLayout() {
	p.ForceHTTPS()

	defer p.Output("ListLayout")

	body := p.Ctx.Input.RequestBody
	qs, _ := url.ParseQuery(string(body))
	start, _ := strconv.Atoi(qs.Get("start"))
	length, _ := strconv.Atoi(qs.Get("length"))

	req := &adoper.ShowItemReq{}
	req.PageIndex = start / length
	req.PageSize = length
	req.Role = false
	resp := &adoper.ShowItemResp{}

	adoper.GetAdvertoperAdminService().ShowLayouts(req, resp)
	p.Resp["code"] = resp.Code
	p.Resp["msg"] = resp.Msg
	if resp.Code == 0 {
		p.Resp["data"] = resp.Data
		p.Resp["has_more"] = resp.HasMore
		p.Resp["recordsTotal"] = resp.Total
		p.Resp["recordsFiltered"] = resp.Total
		p.Resp["draw"], _ = strconv.Atoi(qs.Get("draw"))

	}
}

//新增广告运营位
// @router /layout/save [post]
func (p *AdvertoperAdminController) SaveAdvertoperLayout() {
	p.ForceHTTPS()
	defer p.Output("SaveAdvertoperLayout")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	reqBody := &adoper.LayoutInfo{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	code := adoper.GetAdvertoperAdminService().SaveLayoutRecord(reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)
}

//修改广告运营位
// @router /layout/modify [post]
func (p *AdvertoperAdminController) ModifyAdvertoperLayout() {
	p.ForceHTTPS()
	defer p.Output("ModifyAdvertoperLayout")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	reqBody := &adoper.LayoutInfo{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	code := adoper.GetAdvertoperAdminService().ModifyLayout(reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)
}

//删除广告运营位展示信息
// @router /delete [post]
func (p *AdvertoperAdminController) DeleteAdvertoperItem() {
	p.ForceHTTPS()
	defer p.Output("DeleteAdvertoperLayout")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	req := &struct {
		Appkey   string `json:"appkey"`
		Position int    `json:"position"`
		Id       int    `json:"id"`
		Action   int    `json:"action"`
	}{}

	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	code := adoper.GetAdvertoperAdminService().DeleteAdvertItem(req.Appkey, req.Position, req.Id, req.Action)
	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)
}

//查看特定app特定运营位置的元素数据
// @router /layout/data [post]
func (p *AdvertoperAdminController) ListPostionData() {
	p.ForceHTTPS()

	defer p.Output("ListPostionData")

	body := p.Ctx.Input.RequestBody
	qs, _ := url.ParseQuery(string(body))
	start, _ := strconv.Atoi(qs.Get("start"))
	length, _ := strconv.Atoi(qs.Get("length"))
	ids := qs.Get("ids")
	action, _ := strconv.Atoi(qs.Get("action"))
	log.Debugf("AdvertoperAdminController-ListPostionData; ids:%s, action:%d", ids, action)

	req := &adoper.PositionDataReq{}
	req.PageIndex = start / length
	req.PageSize = length
	req.Ids = ids
	req.Action = action
	req.Role = false
	resp := &adoper.ShowItemResp{}

	adoper.GetAdvertoperAdminService().ShowPositionData(req, resp)
	p.Resp["code"] = resp.Code
	p.Resp["msg"] = resp.Msg
	if resp.Code == 0 {
		p.Resp["data"] = resp.Data
		p.Resp["has_more"] = resp.HasMore
		p.Resp["recordsTotal"] = resp.Total
		p.Resp["recordsFiltered"] = resp.Total
		p.Resp["draw"], _ = strconv.Atoi(qs.Get("draw"))

	}
}
